<template>
  <div class="goods-list">

    <!-- 路由方式一 -->
    <!-- <div class="item" v-for="item in goodsList" :key="item.id">
      <router-link :to="'/home/goodsinfo/' + item.id"><img :src="item.img_url" alt=""></router-link>
      <div class="info">
        <div class="title">{{item.title}}</div>
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">￥{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div> -->

    <!-- 在网页中，有两种跳转方式 -->
    <!-- 方式1：使用 a 标签 的形式叫做 标签跳转 -->
    <!-- 方式2：只用 window.location.href 的形式，叫做 编程式导航 -->

    <!-- item begin -->
    <div class="item" v-for="item in goodsList" :key="item.id" @click="goDetail(item.id)">
      <!-- 产品图片 -->
      <img :src="item.img_url" alt="">
      <!-- 商品价格 begin -->
      <div class="info">
        <!-- 产品标题 -->
        <div class="title">{{item.title}}</div>
        <!-- 产品价格 -->
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">￥{{item.market_price}}</span>
        </p>
        <!-- 产品剩余数 -->
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
  
      </div>
      <!-- 商品价格 end -->
    </div>
    <!-- item end -->

    <!-- 加载更多按钮 -->
    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>

  </div>
</template>

<script>

export default {
    data() {
        return {
            goodsList: [],
            pageindex: 1
        };
    },
    created() {
        this.getGoodsList();
    },
    methods: {
        // 获取产品列表
        getGoodsList() {
            this.$http
                .get("api/getgoods?pageindex=" + this.pageindex)
                .then(result => {
                    if (result.body.status === 0) {
                        this.goodsList = this.goodsList.concat(
                            result.body.message
                        );
                    } else {
                        Toast("获取产品列表失败！");
                    }
                });
        },
        // 加载更多
        getMore() {
            this.pageindex++;
            this.getGoodsList();
        },
        goDetail(id) {
            // 传递字符串
            // this.$router.push("/home/goodsInfo/" + id);
            // 传递对象
            // this.$router.push({ path: "/home/goodsInfo/" + id });
            // 命名的路由
            this.$router.push({ name: "goodsInfo", params: { id } });
            // 带查询参数，变成 /register?plan=private
            // this.$router.push({ path: "/home/goodsInfo/" + id, query: { plan: "private" } });
        }
    }
};

</script>

<style lang="scss">
  .goods-list {
    padding: 7px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .item {
      min-height: 290px;
      padding: 2px;
      width: 49%;
      border: 1px solid #ccc;
      box-shadow: 0 0 8px #ccc;
      margin-bottom: 7px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      img {
        width: 100%;
        height: auto;
      }
      .title {
        font-size: 14px;
      }
    }
    .info {
      font-size: 12px;
      background-color: #eee;
      p {
        margin: 0;
        padding: 0;
      }
      .price {
        .now {
          color: red;
          font-weight: 700;
          font-size: 16px;
        }
        .old {
          font-size: 12px;
          margin-left: 10px;
          text-decoration: line-through;
        }
      }
      .sell {
        font-size: 12px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
</style>
